<?php
/***********************************************************
   KEINOS Template engine class

   Copyright (c) 2008, KEINOS SYSTEM & The Design Team.

   License: GNU Lesser General Public License.
   Please read the "cDocument.readme.inc" for usage.
   Requires Smarty. See http://smarty.php.net/.
************************************************************/

/* ===============================
    テンプレートエンジン(Smarty)の設定
   =============================== */
require_once(check_smarty(TRUE));
if(!defined('KEINOS_TEMPLATE_DIR')) { die("テンプレート・ディレクトリが定義されていません。"); }
if(!defined('KEINOS_COMPILE_DIR'))  { die("コンパイル・ディレクトリが定義されていません。"); }
if(!defined('KEINOS_CACHE_DIR'))    { die("キャッシュ・ディレクトリが定義されていません。"); }
if(!defined('DEBUG_MODE_FORCE'))    { define("DEBUG_MODE_FORCE", FALSE ); }
if(!defined('CACHING'))             { define("CACHING", FALSE );}
if(!defined('BASE_HREF'))           { define("BASE_HREF", "http://{$_SERVER['HTTP_HOST']}/"); }
if(!defined('BASE_HREF_SECURE'))    { define("BASE_HREF_SECURE", "https://{$_SERVER['HTTP_HOST']}/"); }

require_once("class.cyui_css.inc");

class cDocument extends Smarty{
    var $aData = array();
    var $sCacheID = "";
    var $last_modified = "";

    //コンストラクタ＆初期化
    function cDocument(){
        global $_SERVER;

        $this->Smarty();
        $this->cache_modified_check = true;
        $this->template_dir         = KEINOS_TEMPLATE_DIR;
        $this->compile_dir          = KEINOS_COMPILE_DIR;
        $this->cache_dir            = KEINOS_CACHE_DIR;
        $this->debugging            = DEBUG_MODE_FORCE;
        $this->left_delimiter       = "<{";
        $this->right_delimiter      = "}>";
        $this->sCacheID             = md5($_SERVER["REQUEST_URI"]);
        $this->caching              = CACHING;
        $this->compile_check        = !(CACHING);
        $this->cache_modified_check = CACHING; //304
        if(CACHING){ session_start(); }
    }

    function _sanitize_template_data($sData){
        return $sData;
    }

    function list_template($sExt=""){
        return listdir($this->template_dir,$sExt);
    }

    function get_template($sTemplateName){
        $sPathTemplate = $this->template_dir . $sTemplateName;
        if(File_Exists($sPathTemplate)){
            return file_get_contents($sPathTemplate);
        }
        else{
            return "Template '{$sTemplateName}' not found at {$this->template_dir}.";
        }
    }

    function put_template($sTemplateName,$sData){
        $sPathTemplate = $this->template_dir . $sTemplateName;
        return file_put_contents( $sPathTemplate, $this->_sanitize_template_data($sData) );
    }

    function delete_template($sTemplateName,$bCompleteDelete=FALSE){
        $sPathTemplate = $this->template_dir . $sTemplateName;
        if(File_Exists($sPathTemplate)){
            if(!$bCompleteDelete){
                return $this->rename_template($sTemplateName, $sTemplateName . ".deleted");
            }
            else{
                return unlink($sPathTemplate);
            }
        }
        else{
            return FALSE;
        }
    }

    function rename_template($sOldTemplateName,$sNewTemplateName){
        $sPathTemplate_old = $this->template_dir . $sOldTemplateName;
        $sPathTemplate_new = $this->template_dir . $sNewTemplateName;
        if(File_Exists($sPathTemplate_old)){
            return rename($sPathTemplate_old,$sPathTemplate_new);
        }
        else{
            return FALSE;
        }
    }

    function Data($key="",$value=NULL){
        if(Empty($key)) die('You need to set a key. (cDocument.inc, Data)');
        return ($this->aData[$key] = $value);
    }
    
    function pre_assign($s=""){
        $oYUI_CSS = new cYUI_CSS();

        $this -> assign("YUI",$oYUI_CSS);
        $this -> assign("base_href", BASE_HREF);
        $this -> assign("base_href_secure", BASE_HREF_SECURE);
        $this -> assign("IsMobile", fIsMobile());
        $this -> assign("data",$this->aData);
        $this -> assign("uniqid",uniqid("_"));
    }

    function show($sTemplate="",$sCacheID="",$bShowAsText=FALSE){
        global $_SERVER;

        if( CACHING ) $this->caching = 2;

        $sCacheID    = Empty($sCacheID)    ? $this->sCacheID : $sCacheID;
        $bShowAsText = Empty($bShowAsText) ? FALSE           : TRUE;

        $this->_smarty_md5 = md5($sCacheID . $_SERVER["SERVER_ADDR"]);
        $this->pre_assign($sTemplate);
        
        if($bShowAsText){
            return $this->fetch($sTemplate,$sCacheID);
        }
        else{
            $this -> display($sTemplate,$sCacheID);
        }
    }

    function show_cache($sTemplate="",$sCacheID=""){
        if( CACHING ) $this->caching = 2;

        if($this->is_cached($sTemplate,$sCacheID) && CACHING){
            echo $this->fetch($sTemplate,$sCacheID);
            die();
        }
        
    }

}
?>